import { useState, useEffect } from 'react';
import { get, post } from '@/lib/request';

function useFetchBooksAndAccounts() {
  const [bookOptions, setBookOptions] = useState([]);
  const [loginUser, setLoginUser] = useState();
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState(false);

  useEffect(() => {
    setLoading(true);
    const fetchData = async () => {
      try {
        // Fetch messages and account-books data
        const [bookOptions, userData] = await Promise.all([
          get('/api/funds-options'),
          get('/api/user'), 
        ]);
        setBookOptions(bookOptions);
        setLoginUser(userData);
      } catch (err) {
        console.error(err);
        setError(true);
      } finally {
        setLoading(false);
      }
    };

    fetchData();
    // Cleanup function
    return () => {
      setBookOptions([]);
    };
  }, []);

  return { bookOptions, loading, error, loginUser };
}

export default useFetchBooksAndAccounts;